package cn.azzhu.day06;

import cn.azzhu.utils.FlinkUtils;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.util.Collector;

/**
 * @author azzhu
 * @create 2020-09-20 11:45:27
 */
public class FlinkKafkaToRedis {
    public static void main(String[] args) throws Exception {
//        final ParameterTool parameterTool = ParameterTool.fromArgs(args);
//        final String groupId = parameterTool.get("group.id","g10");
//        final String topics = parameterTool.getRequired("topics");
          final ParameterTool parameters = ParameterTool.fromPropertiesFile("D:\\bigdata\\flink-learning\\src\\main\\resources\\config.properties");

        final DataStream<String> lines = FlinkUtils.createKafkaStream(parameters, SimpleStringSchema.class);

        final SingleOutputStreamOperator<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String line, Collector<String> out) throws Exception {
                final String[] words = line.split(" ");
                for (String word : words) {
                    out.collect(word);
                }
            }
        });

        //(word,1)
        final SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String word) throws Exception {
                return Tuple2.of(word, 1);
            }
        });
        final SingleOutputStreamOperator<Tuple2<String, Integer>> summed = wordAndOne.keyBy(0).sum(1);

        //写入到redis
        summed.map(new MapFunction<Tuple2<String, Integer>, Tuple3<String, String,String>>() {
            @Override
            public Tuple3<String, String, String> map(Tuple2<String, Integer> tp) throws Exception {
                return Tuple3.of("WORD_COUNT",tp.f0,tp.f1.toString());
            }
        }).addSink(new MyRedisSink());

        FlinkUtils.getEnv().execute("FlinkKafkaToRedis");
    }
}
